{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Mini Parser"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #stack #depth-first-search #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #栈 #深度优先搜索 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: deserialize"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #迷你语法分析器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个字符串 s 表示一个整数嵌套列表，实现一个解析它的语法分析器并返回解析的结果&nbsp;<code>NestedInteger</code> 。</p>\n",
    "\n",
    "<p>列表中的每个元素只可能是整数或整数嵌套列表</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"324\",\n",
    "<strong>输出：</strong>324\n",
    "<strong>解释：</strong>你应该返回一个 NestedInteger 对象，其中只包含整数值 324。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"[123,[456,[789]]]\",\n",
    "<strong>输出：</strong>[123,[456,[789]]]\n",
    "<strong>解释：</strong>返回一个 NestedInteger 对象包含一个有两个元素的嵌套列表：\n",
    "1. 一个 integer 包含值 123\n",
    "2. 一个包含两个元素的嵌套列表：\n",
    "    i.  一个 integer 包含值 456\n",
    "    ii. 一个包含一个元素的嵌套列表\n",
    "         a. 一个 integer 包含值 789\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li>\n",
    "\t<li><code>s</code> 由数字、方括号&nbsp;<code>\"[]\"</code>、负号&nbsp;<code>'-'</code>&nbsp;、逗号&nbsp;<code>','</code>组成</li>\n",
    "\t<li>用例保证&nbsp;<code>s</code> 是可解析的&nbsp;<code>NestedInteger</code></li>\n",
    "\t<li>输入中的所有值的范围是&nbsp;<code>[-10<sup>6</sup>, 10<sup>6</sup>]</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [mini-parser](https://leetcode.cn/problems/mini-parser/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [mini-parser](https://leetcode.cn/problems/mini-parser/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"324\"', '\"[123,[456,[789]]]\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger: \n",
    "        if s[0] != '[':\n",
    "            return NestedInteger(int(s))\n",
    "\n",
    "        stack, sign, cur_val = [], False, 0\n",
    "        \n",
    "        for i, c in enumerate(s):\n",
    "            if c == \"[\":\n",
    "                stack.append([])\n",
    "                continue\n",
    "            if c == '-':\n",
    "                sign = True\n",
    "                continue\n",
    "            if c == \",\" :\n",
    "                if s[i - 1].isdigit():\n",
    "                    stack[-1].append(-cur_val if sign else cur_val) \n",
    "                cur_val, sign = 0, False\n",
    "                continue\n",
    "            if c == \"]\" :\n",
    "                if s[i-1].isdigit():\n",
    "                    stack[-1].append(-cur_val if sign else cur_val)\n",
    "                cur_val, sign = 0, False\n",
    "                if len(stack) > 1:\n",
    "                    cur = stack.pop()\n",
    "                    stack[-1].append(cur)\n",
    "                continue\n",
    "\n",
    "            cur_val = cur_val * 10 + ord(c) - ord('0')\n",
    "        return NestedInteger(stack.pop())\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        return NestedInteger(json.loads(s))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        stack = []\n",
    "        for w in s:\n",
    "            if not stack or w in \"[-,\":\n",
    "                stack.append(w)\n",
    "            elif w in \"0123456789\":\n",
    "                if isinstance(stack[-1], str) and stack[-1][-1] in \"-0123456789\":\n",
    "                    stack[-1] += w\n",
    "                else:\n",
    "                    stack.append(w)\n",
    "            else:\n",
    "                tmp = []\n",
    "                while stack[-1] != '[':\n",
    "                    if stack[-1] == ',':\n",
    "                        stack.pop()\n",
    "                    elif isinstance(stack[-1], str):\n",
    "                        tmp.append(int(stack.pop()))\n",
    "                    else:\n",
    "                        tmp.append(stack.pop())      \n",
    "                stack.pop()\n",
    "                stack.append(tmp[::-1])\n",
    "                    \n",
    "        return NestedInteger(int(stack[0]) if isinstance(stack[0], str) else stack[0])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        def recur(s):\n",
    "            if s=='':    \n",
    "                return None        \n",
    "            if s[0]!='[':\n",
    "                return int(s)\n",
    "            cur_depth=0\n",
    "            left=1\n",
    "            right=0\n",
    "            result=[]\n",
    "            while right<len(s)-1:\n",
    "                right+=1\n",
    "                if s[right]=='[':\n",
    "                    cur_depth+=1\n",
    "                elif s[right]==']':\n",
    "                    cur_depth-=1\n",
    "                elif s[right]==',' and cur_depth==0:\n",
    "                    r=recur(s[left:right])\n",
    "                    if r is not None:\n",
    "                        result.append(r)\n",
    "                    left=right+1\n",
    "                if cur_depth==-1:\n",
    "                    r=recur(s[left:right])\n",
    "                    if r is not None:\n",
    "                        result.append(r)\n",
    "            return result\n",
    "\n",
    "        return NestedInteger(recur(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        return NestedInteger(json.loads(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        if s.lstrip('-').isdecimal():\n",
    "            return NestedInteger(int(s))\n",
    "        else:\n",
    "            stack = []\n",
    "            i = 0\n",
    "            while i < len(s):\n",
    "                # print('---1---', s[i], stack)\n",
    "                if s[i] == '[':\n",
    "                    stack.append([])\n",
    "                elif s[i] == ']':\n",
    "                    # print('---2---', stack)\n",
    "                    if len(stack) > 1:\n",
    "                        stack[-2].append(stack.pop())\n",
    "                elif s[i] == '-' or s[i].isdecimal():\n",
    "                    num = ''\n",
    "                    while s[i] == '-' or s[i].isdecimal():\n",
    "                        num += s[i]\n",
    "                        i += 1\n",
    "                    i -= 1  # i多加了一个, 再减一下\n",
    "                    stack[-1].append(int(num))\n",
    "                i += 1\n",
    "            return NestedInteger(stack[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        if s.lstrip('-').isdecimal():\n",
    "            return NestedInteger(int(s))\n",
    "        else:\n",
    "            stack = []\n",
    "            i = 0\n",
    "            while i < len(s):\n",
    "                # print('---1---', s[i], stack)\n",
    "                if s[i] == '[':\n",
    "                    stack.append([])\n",
    "                elif s[i] == ']':\n",
    "                    # print('---2---', stack)\n",
    "                    if len(stack) > 1:\n",
    "                        stack[-2].append(stack.pop())\n",
    "                elif s[i] == '-' or s[i].isdecimal():\n",
    "                    num = ''\n",
    "                    while s[i] == '-' or s[i].isdecimal():\n",
    "                        num += s[i]\n",
    "                        i += 1\n",
    "                    i -= 1  # i多加了一个, 再减一下\n",
    "                    stack[-1].append(int(num))\n",
    "                i += 1\n",
    "            return NestedInteger(stack[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        stack = []\n",
    "\n",
    "        for w in s:\n",
    "            #print('\\n',stack, ' ------ ',w)\n",
    "            if not stack or w in \"[-,\":\n",
    "                stack.append(w)\n",
    "            elif w in \"0123456789\":\n",
    "                #print(stack)\n",
    "                if isinstance(stack[-1], str) and stack[-1][-1] in \"-0123456789\":\n",
    "                    stack[-1] += w\n",
    "                else:\n",
    "                    stack.append(w)\n",
    "\n",
    "                \n",
    "            else:\n",
    "                #print(']')\n",
    "                tmp = []\n",
    "                \n",
    "                while stack[-1] != '[':\n",
    "                    if stack[-1] == ',':\n",
    "                        stack.pop()\n",
    "                    elif isinstance(stack[-1], str):\n",
    "                        tmp.append(int(stack.pop()))\n",
    "                    else:\n",
    "                        tmp.append(stack.pop())\n",
    "                        \n",
    "                stack.pop()\n",
    "                stack.append(tmp[::-1])\n",
    "            #print('=> ', stack)\n",
    "                    \n",
    "        return NestedInteger(int(stack[0]) if isinstance(stack[0], str) else stack[0])\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        def recur(s):\n",
    "            print('SSSS',s)\n",
    "            if s=='':    \n",
    "                return None        \n",
    "            if s[0]!='[':\n",
    "                return int(s)\n",
    "            cur_depth=0\n",
    "            left=1\n",
    "            right=0\n",
    "            result=[]\n",
    "            while right<len(s)-1:\n",
    "                right+=1\n",
    "                if s[right]=='[':\n",
    "                    cur_depth+=1\n",
    "                elif s[right]==']':\n",
    "                    cur_depth-=1\n",
    "                elif s[right]==',' and cur_depth==0:\n",
    "                    r=recur(s[left:right])\n",
    "                    if r is not None:\n",
    "                        result.append(r)\n",
    "                    left=right+1\n",
    "                if cur_depth==-1:\n",
    "                    r=recur(s[left:right])\n",
    "                    if r is not None:\n",
    "                        result.append(r)\n",
    "            print('RRRR',result)\n",
    "            return result\n",
    "\n",
    "        return NestedInteger(recur(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        index = 0\n",
    "\n",
    "        def dfs() -> NestedInteger:\n",
    "            nonlocal index\n",
    "            if s[index] == '[':\n",
    "                index += 1\n",
    "                ni = NestedInteger()\n",
    "                while s[index] != ']':\n",
    "                    ni.add(dfs())\n",
    "                    if s[index] == ',':\n",
    "                        index += 1\n",
    "                index += 1\n",
    "                return ni\n",
    "            else:\n",
    "                negative = False\n",
    "                if s[index] == '-':\n",
    "                    negative = True\n",
    "                    index += 1\n",
    "                num = 0\n",
    "                while index < len(s) and s[index].isdigit():\n",
    "                    num *= 10\n",
    "                    num += int(s[index])\n",
    "                    index += 1\n",
    "                if negative:\n",
    "                    num = -num\n",
    "                return NestedInteger(num)\n",
    "\n",
    "        return dfs()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        index = 0\n",
    "        def dfs() -> NestedInteger:\n",
    "            nonlocal index\n",
    "            if s[index] == '[':\n",
    "                index += 1\n",
    "                ni = NestedInteger()\n",
    "                while s[index] != ']':\n",
    "                    ni.add(dfs())\n",
    "                    if s[index] == ',':\n",
    "                        index += 1\n",
    "                index += 1\n",
    "                return ni\n",
    "            else:\n",
    "                tempString = ''\n",
    "                while index < len(s) and (s[index].isdigit() or s[index] == \"-\"):\n",
    "                    tempString = tempString + s[index]\n",
    "                    index += 1\n",
    "                num = int(tempString)\n",
    "                return NestedInteger(num)\n",
    "\n",
    "        return dfs()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def deserialize(self, s: str):\n",
    "        ans, stack = None, []\n",
    "        num = ''\n",
    "        s += ','\n",
    "        for c in s:\n",
    "            if c == '[':\n",
    "                stack.append(NestedInteger())\n",
    "            elif c.isdigit() or c == '-':\n",
    "                num += c\n",
    "            elif c == ',':\n",
    "                if num != '':\n",
    "                    if len(stack):\n",
    "                        stack[-1].add(NestedInteger(int(num)))\n",
    "                    else:\n",
    "                        ans = NestedInteger(int(num))\n",
    "                    num = ''\n",
    "            elif c == ']':\n",
    "                if num != '':\n",
    "                    stack[-1].add(NestedInteger(int(num)))\n",
    "                    num = ''\n",
    "                top = stack.pop()\n",
    "                if len(stack):\n",
    "                    stack[-1].add(top)\n",
    "                else:\n",
    "                    ans = top\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        def isDigit(ch):\n",
    "            return ord(ch) >= ord('0') and ord(ch) <= ord('9')\n",
    "        nest = NestedInteger()\n",
    "        if s[0] != '[':\n",
    "            nest.setInteger(int(s))\n",
    "            return nest\n",
    "\n",
    "        stack = []\n",
    "        num = 0\n",
    "        neg_flag = False\n",
    "        for p in range(len(s)):\n",
    "            if s[p] == '[':\n",
    "                tmp_nest = NestedInteger()\n",
    "                stack.append(tmp_nest)\n",
    "                num = 0\n",
    "                neg_flag = False\n",
    "            elif s[p] == '-':\n",
    "                num = 0\n",
    "                neg_flag = True\n",
    "            elif s[p] == ']':\n",
    "                if isDigit(s[p-1]):\n",
    "                    if neg_flag == True:\n",
    "                        num = -num\n",
    "                    tmp_nest.add(NestedInteger(num))\n",
    "                    num = 0\n",
    "                tmp_nest = stack.pop()\n",
    "                if len(stack) == 0:\n",
    "                    return tmp_nest\n",
    "                else:\n",
    "                    stack[-1].add(tmp_nest)\n",
    "                    tmp_nest = stack[-1]\n",
    "            elif s[p] == ',':\n",
    "                if isDigit(s[p-1]):\n",
    "                    if neg_flag == True:\n",
    "                        num = -num\n",
    "                    tmp_nest.add(NestedInteger(num))\n",
    "                num = 0\n",
    "                neg_flag = False\n",
    "            else:\n",
    "                num = num * 10 + ord(s[p]) - ord('0')\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        index = 0\n",
    "        def dfs() -> NestedInteger:\n",
    "            nonlocal index\n",
    "            if s[index] == '[':\n",
    "                index += 1\n",
    "                ni = NestedInteger()\n",
    "                while s[index] != ']':\n",
    "                    ni.add(dfs())\n",
    "                    if s[index] == ',':\n",
    "                        index += 1\n",
    "                index += 1\n",
    "                return ni\n",
    "            else:\n",
    "                negative = False\n",
    "                if s[index] == '-':\n",
    "                    negative = True\n",
    "                    index += 1\n",
    "                num = 0\n",
    "                while index < len(s) and s[index].isdigit():\n",
    "                    num *= 10\n",
    "                    num += int(s[index])\n",
    "                    index += 1\n",
    "                if negative:\n",
    "                    num = -num\n",
    "                return NestedInteger(num)\n",
    "\n",
    "        return dfs()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\r\n",
    "# This is the interface that allows for creating nested lists.\r\n",
    "# You should not implement it, or speculate about its implementation\r\n",
    "# \"\"\"\r\n",
    "#class NestedInteger:\r\n",
    "#    def __init__(self, value=None):\r\n",
    "#        \"\"\"\r\n",
    "#        If value is not specified, initializes an empty list.\r\n",
    "#        Otherwise initializes a single integer equal to value.\r\n",
    "#        \"\"\"\r\n",
    "#\r\n",
    "#    def isInteger(self):\r\n",
    "#        \"\"\"\r\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\r\n",
    "#        :rtype bool\r\n",
    "#        \"\"\"\r\n",
    "#\r\n",
    "#    def add(self, elem):\r\n",
    "#        \"\"\"\r\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\r\n",
    "#        :rtype void\r\n",
    "#        \"\"\"\r\n",
    "#\r\n",
    "#    def setInteger(self, value):\r\n",
    "#        \"\"\"\r\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\r\n",
    "#        :rtype void\r\n",
    "#        \"\"\"\r\n",
    "#\r\n",
    "#    def getInteger(self):\r\n",
    "#        \"\"\"\r\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\r\n",
    "#        Return None if this NestedInteger holds a nested list\r\n",
    "#        :rtype int\r\n",
    "#        \"\"\"\r\n",
    "#\r\n",
    "#    def getList(self):\r\n",
    "#        \"\"\"\r\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\r\n",
    "#        Return None if this NestedInteger holds a single integer\r\n",
    "#        :rtype List[NestedInteger]\r\n",
    "#        \"\"\"\r\n",
    "class Solution:\r\n",
    "    def deserialize(self, s: str) -> NestedInteger:\r\n",
    "        if s[0] != '[':\r\n",
    "            return NestedInteger(int(s))\r\n",
    "        stack, num, negative = [], 0, False\r\n",
    "        for i, c in enumerate(s):\r\n",
    "            if c == '-':\r\n",
    "                negative = True\r\n",
    "            elif c.isdigit():\r\n",
    "                num = num * 10 + int(c)\r\n",
    "            elif c == '[':\r\n",
    "                stack.append(NestedInteger())\r\n",
    "            elif c in ',]':\r\n",
    "                if s[i-1].isdigit():\r\n",
    "                    if negative:\r\n",
    "                        num = -num\r\n",
    "                    stack[-1].add(NestedInteger(num))\r\n",
    "                num, negative = 0, False\r\n",
    "                if c == ']' and len(stack) > 1:\r\n",
    "                    stack[-2].add(stack.pop())\r\n",
    "        return stack.pop()\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        if s[0] != '[':\n",
    "            return NestedInteger(int(s))\n",
    "        st , num , neg = [],0,False\n",
    "        for i,c in enumerate(s):\n",
    "            if c == '-':\n",
    "                neg = True\n",
    "            elif c.isdigit():\n",
    "                num = num * 10 + int(c)\n",
    "            elif c=='[':\n",
    "                st.append(NestedInteger())\n",
    "            elif c in ',]':\n",
    "                if s[i-1].isdigit():\n",
    "                    if neg:\n",
    "                        num = -num\n",
    "                    st[-1].add(NestedInteger(num))\n",
    "                num , neg = 0 , False\n",
    "            if c == ']' and len(st) > 1:\n",
    "                st[-2].add(st.pop())\n",
    "        return st.pop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        stack, num, last = [], \"\", \"\"\n",
    "        for c in s:\n",
    "            if c in set('-0123456789'): \n",
    "                num += c\n",
    "            elif c == \",\" and num:\n",
    "                stack[-1].add(NestedInteger(int(num)))\n",
    "                num = \"\"\n",
    "            elif c == \"[\":\n",
    "                elem = NestedInteger()\n",
    "                if stack: \n",
    "                    stack[-1].add(elem)\n",
    "                stack.append(elem)\n",
    "            elif c == \"]\":\n",
    "                if num:\n",
    "                    stack[-1].add(NestedInteger(int(num)))\n",
    "                    num = \"\"\n",
    "                last = stack.pop()\n",
    "        return last if last else NestedInteger(int(num))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        if s[0] != '[':\n",
    "            return NestedInteger(int(s))\n",
    "        stack, num, negative = [], 0, False\n",
    "        for i, c in enumerate(s):\n",
    "            if c == '-':\n",
    "                negative = True\n",
    "            elif c.isdigit():\n",
    "                num = num * 10 + int(c)\n",
    "            elif c == '[':\n",
    "                stack.append(NestedInteger())\n",
    "            elif c in ',]':\n",
    "                if s[i-1].isdigit():\n",
    "                    if negative:\n",
    "                        num = -num\n",
    "                    stack[-1].add(NestedInteger(num))\n",
    "                num, negative = 0, False\n",
    "                if c == ']' and len(stack) > 1:\n",
    "                    stack[-2].add(stack.pop())\n",
    "        return stack.pop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        if s[0] != '[': return NestedInteger(int(s))\n",
    "\n",
    "        stack = []\n",
    "        num, sign, is_num = 0, 1, False\n",
    "        for i in s:\n",
    "            if i == '-':\n",
    "                sign = -1\n",
    "            elif i.isdigit():\n",
    "                num = 10*num + int(i)\n",
    "                is_num = True\n",
    "            elif i == '[':\n",
    "                stack.append(NestedInteger())\n",
    "            elif i == ']' or i == ',':\n",
    "                if is_num:\n",
    "                    curl_list = stack.pop()\n",
    "                    curl_list.add(NestedInteger(num*sign))\n",
    "                    stack.append(curl_list)\n",
    "                    num, sign, is_num = 0, 1, False\n",
    "                \n",
    "                if i == ']' and len(stack) > 1:\n",
    "                    curl_list = stack.pop()\n",
    "                    stack[-1].add(curl_list)\n",
    "        return stack[0]    \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        stack, num, last = [], \"\", \"\"\n",
    "        for c in s:\n",
    "            if c in set('-0123456789'): \n",
    "                num += c\n",
    "            elif c == \",\" and num:\n",
    "                stack[-1].add(NestedInteger(int(num)))\n",
    "                num = \"\"\n",
    "            elif c == \"[\":\n",
    "                elem = NestedInteger()\n",
    "                if stack: \n",
    "                    stack[-1].add(elem)\n",
    "                stack.append(elem)\n",
    "            elif c == \"]\":\n",
    "                if num:\n",
    "                    stack[-1].add(NestedInteger(int(num)))\n",
    "                    num = \"\"\n",
    "                last = stack.pop()\n",
    "        return last if last else NestedInteger(int(num))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        if s[0]!='[':\n",
    "            return NestedInteger(int(s))\n",
    "        stack=[]\n",
    "        num=0\n",
    "        flag=False\n",
    "        for i,c in enumerate(s):\n",
    "            if c=='-':\n",
    "                flag=True\n",
    "            elif c.isdigit():\n",
    "                num=num*10+int(c)\n",
    "            elif c=='[':\n",
    "                stack.append(NestedInteger())\n",
    "            elif c in ',]':\n",
    "                if s[i-1].isdigit():\n",
    "                    if flag:\n",
    "                        num=-num\n",
    "                    stack[-1].add(NestedInteger(num))\n",
    "                num=0\n",
    "                flag=False\n",
    "                if c==']'and len(stack)>1:\n",
    "                    stack[-2].add(stack.pop())\n",
    "        return stack.pop()\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        if '[' not in s:\n",
    "            return NestedInteger(int(s))\n",
    "\n",
    "        stack=[]\n",
    "        idx=0\n",
    "        while idx<len(s):\n",
    "            # 从左往右开始解析\n",
    "            if s[idx]=='[':\n",
    "                # 遇到了左括号，stack中增加一层NestedInteger对象\n",
    "                stack.append(NestedInteger())\n",
    "                idx+=1\n",
    "            elif s[idx]==']':\n",
    "                # 遇到了右括号，如果stack层数大于1，将stack的最后一层弹出，并加入到下一层\n",
    "                if len(stack)>1:\n",
    "                    nested_integer=stack.pop()\n",
    "                    stack[-1].add(nested_integer)\n",
    "                idx+=1\n",
    "            elif s[idx]==',':\n",
    "                idx+=1\n",
    "            else:\n",
    "                # 对字符串进行处理\n",
    "                start=idx\n",
    "                while idx<len(s) and s[idx] not in '[],':\n",
    "                    idx+=1\n",
    "                num=int(s[start:idx])\n",
    "                # 将解析出的数字放入stack最上层的NestedInteger中\n",
    "                stack[-1].add(NestedInteger(num))\n",
    "        return stack[-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        if s[0] != '[': return NestedInteger(int(s))\n",
    "\n",
    "        stack = []\n",
    "        num, sign, is_num = 0, 1, False\n",
    "        for i in s:\n",
    "            if i == '-':\n",
    "                sign = -1\n",
    "            elif i.isdigit():\n",
    "                num = 10*num + int(i)\n",
    "                is_num = True\n",
    "            elif i == '[':\n",
    "                stack.append(NestedInteger())\n",
    "            elif i == ']' or i == ',':\n",
    "                if is_num:\n",
    "                    curl_list = stack.pop()\n",
    "                    curl_list.add(NestedInteger(num*sign))\n",
    "                    stack.append(curl_list)\n",
    "                num, sign, is_num = 0, 1, False\n",
    "                \n",
    "                if i == ']' and len(stack) > 1:\n",
    "                    curl_list = stack.pop()\n",
    "                    stack[-1].add(curl_list)\n",
    "        return stack[0]    \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        \n",
    "        if s[0] != '[':\n",
    "            return NestedInteger(int(s))\n",
    "        \n",
    "        stack = []\n",
    "        # num为数字，sign为符号为，is_num为前一个是否为数字\n",
    "        num, sign, is_num = 0, 1, False\n",
    "        \n",
    "        for c in s:\n",
    "            if c.isdigit():\n",
    "                num = num * 10 + int(c)\n",
    "                is_num = True\n",
    "            elif c == '-':\n",
    "                sign = -1\n",
    "            elif c == '[':\n",
    "                stack.append(NestedInteger())\n",
    "            elif c == ',' or c == ']':\n",
    "                # 把刚才遇到的数字append进去\n",
    "                if is_num:\n",
    "                    cur_list = stack.pop()\n",
    "                    cur_list.add(NestedInteger(sign * num))\n",
    "                    stack.append(cur_list)\n",
    "                num, sign, is_num = 0, 1, False\n",
    "\n",
    "                # 此时为嵌套列表\n",
    "                if c == ']' and len(stack) > 1:\n",
    "                    cur_list = stack.pop()\n",
    "                    stack[-1].add(cur_list)\n",
    "\n",
    "        return stack[0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        # self.idx = 0\n",
    "\n",
    "        # def dfs():\n",
    "        #     if s[self.idx] == '[':\n",
    "        #         ni = NestedInteger()\n",
    "        #         self.idx += 1\n",
    "        #         while s[self.idx] != ']':\n",
    "        #             ni.add(dfs())\n",
    "        #             if s[self.idx] == ',':\n",
    "        #                 self.idx += 1\n",
    "        #         self.idx += 1\n",
    "        #         return ni\n",
    "        #     else:\n",
    "        #         negative = False\n",
    "        #         if s[self.idx] == '-': \n",
    "        #             negative = True\n",
    "        #             self.idx += 1\n",
    "        #         num = 0\n",
    "        #         while self.idx < len(s) and s[self.idx].isdigit():\n",
    "        #             num = 10 * num + int(s[self.idx])\n",
    "        #             self.idx += 1\n",
    "        #         if negative: num = -num\n",
    "        #         return NestedInteger(num)\n",
    "        \n",
    "        # return dfs()\n",
    "        if s[0] != '[': return NestedInteger(int(s))\n",
    "        stk = []\n",
    "        num = 0\n",
    "        negative = False\n",
    "        for i, c in enumerate(s):\n",
    "            if c == '[':\n",
    "                stk.append(NestedInteger())\n",
    "            elif c.isdigit():\n",
    "                num = num * 10 + int(c)\n",
    "            elif c == '-':\n",
    "                negative = True\n",
    "            else:\n",
    "                if s[i - 1].isdigit(): \n",
    "                    if negative == True: num = -num\n",
    "                    stk[-1].add(NestedInteger(num))\n",
    "                    num = 0\n",
    "                    negative = False\n",
    "                if c == ']' and len(stk) > 1:\n",
    "                    stk[-2].add(stk.pop())\n",
    "        return stk.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        idx = 0\n",
    "        def dfs():\n",
    "            nonlocal idx\n",
    "            print(idx)\n",
    "            if s[idx] == '[':\n",
    "                res = NestedInteger()\n",
    "                idx += 1\n",
    "                while s[idx] != ']':\n",
    "                    res.add(dfs())\n",
    "                    if s[idx] == ',':\n",
    "                        idx += 1\n",
    "                idx += 1                        \n",
    "                return res                        \n",
    "            else:\n",
    "                negative = 1 if s[idx] != '-' else -1\n",
    "                if negative == -1:\n",
    "                    idx += 1\n",
    "                num = 0                    \n",
    "                while idx < len(s) and s[idx].isdigit():\n",
    "                    num = num * 10 + int(s[idx])\n",
    "                    idx += 1\n",
    "                return NestedInteger(num * negative)\n",
    "        return dfs()                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        if s[0] != '[':\n",
    "            return NestedInteger(int(s))\n",
    "        stack, num, negative = [], 0, False\n",
    "        for i, c in enumerate(s):\n",
    "            if c == '-':\n",
    "                negative = True\n",
    "            elif c.isdigit():\n",
    "                num = num * 10 + int(c)\n",
    "            elif c == '[':\n",
    "                stack.append(NestedInteger())\n",
    "            elif c in ',]':\n",
    "                if s[i-1].isdigit():\n",
    "                    if negative:\n",
    "                        num = -num\n",
    "                    stack[-1].add(NestedInteger(num))\n",
    "                num, negative = 0, False\n",
    "                if c == ']' and len(stack) > 1:\n",
    "                    stack[-2].add(stack.pop())\n",
    "        return stack.pop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        stack, elem, hasNum, sgn, num = [], None, False, 1, 0\n",
    "        for x in s:\n",
    "            # 出现[, 则为内嵌的一个整型\n",
    "            if x == '[':\n",
    "                stack.append(NestedInteger())\n",
    "            elif x == '-':\n",
    "                sgn = -1\n",
    "            # 数字\n",
    "            elif x.isdigit():\n",
    "                hasNum = True\n",
    "                num = num * 10 + int(x)\n",
    "            else:\n",
    "                # 如果有数字, 则写入最后一个整型中.\n",
    "                if hasNum:\n",
    "                    stack[-1].add(NestedInteger(num * sgn))\n",
    "                    hasNum, sgn, num = False, 1, 0\n",
    "                # 弹出内部的整型, 需要重新压入(因为还不是最后一个整型)\n",
    "                elif elem is not None:\n",
    "                    stack[-1].add(elem)\n",
    "                    elem = None\n",
    "                # 弹出整型\n",
    "                if x == ']':\n",
    "                    elem = stack.pop()\n",
    "        # 只存在数字的情况\n",
    "        if hasNum:\n",
    "            return NestedInteger(sgn * num)\n",
    "        return elem\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        if s[0] != '[':\n",
    "            return NestedInteger(int(s))\n",
    "        stack, num, negative = [], 0, False\n",
    "        for i, c in enumerate(s):\n",
    "            if c == '-':\n",
    "                negative = True\n",
    "            elif c.isdigit():\n",
    "                num = num * 10 + int(c)\n",
    "            elif c == '[':\n",
    "                stack.append(NestedInteger())\n",
    "            elif c in ',]':\n",
    "                if s[i-1].isdigit():\n",
    "                    if negative:\n",
    "                        num = -num\n",
    "                    stack[-1].add(NestedInteger(num))\n",
    "                num, negative = 0, False\n",
    "                if c == ']' and len(stack) > 1:\n",
    "                    stack[-2].add(stack.pop())\n",
    "        return stack.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        res = NestedInteger()\n",
    "        stack = [res]\n",
    "        t = ''\n",
    "    \n",
    "        def clear():\n",
    "            nonlocal t\n",
    "            if not t: return\n",
    "\n",
    "            stack[-1].add(NestedInteger(int(t)))\n",
    "            t = ''\n",
    "\n",
    "        for item in s:\n",
    "            if item =='[':\n",
    "                l = NestedInteger()\n",
    "                stack[-1].add(l)\n",
    "                stack.append(l)\n",
    "            elif item ==']':\n",
    "                clear()\n",
    "                stack.pop()\n",
    "            elif item == ',':\n",
    "                clear()\n",
    "            else:\n",
    "                t += item\n",
    "        clear()\n",
    " \n",
    "        return res.getList()[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        self.idx = 0\n",
    "\n",
    "        def dfs():\n",
    "            if s[self.idx] == '[':\n",
    "                ni = NestedInteger()\n",
    "                self.idx += 1\n",
    "                while s[self.idx] != ']':\n",
    "                    ni.add(dfs())\n",
    "                    if s[self.idx] == ',':\n",
    "                        self.idx += 1\n",
    "                self.idx += 1\n",
    "                return ni\n",
    "            else:\n",
    "                negative = False\n",
    "                if s[self.idx] == '-': \n",
    "                    negative = True\n",
    "                    self.idx += 1\n",
    "                num = 0\n",
    "                while self.idx < len(s) and s[self.idx].isdigit():\n",
    "                    num = 10 * num + int(s[self.idx])\n",
    "                    self.idx += 1\n",
    "                if negative: num = -num\n",
    "                return NestedInteger(num)\n",
    "        \n",
    "        return dfs()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        stack = []\n",
    "\n",
    "        root = NestedInteger()\n",
    "\n",
    "        if s[0] != '[':\n",
    "            return NestedInteger(int(s))\n",
    "        \n",
    "        stack.append(root)\n",
    "\n",
    "        i = 1\n",
    "\n",
    "        while i < len(s) - 1:\n",
    "            c = s[i]\n",
    "            if c.isdigit() or c == '-':\n",
    "                neg = False\n",
    "                if c == '-':\n",
    "                    neg = True\n",
    "                    i += 1\n",
    "\n",
    "                num = int(s[i])\n",
    "                while i+1 < len(s) and s[i+1].isdigit():\n",
    "                    num *= 10\n",
    "                    num += int(s[i+1])\n",
    "                    i += 1\n",
    "                if neg:\n",
    "                    num = -num\n",
    "                tmp = NestedInteger(num)\n",
    "                stack[-1].add(tmp)\n",
    "                i += 1\n",
    "            elif c == '[':\n",
    "                stack.append(NestedInteger())\n",
    "                i += 1\n",
    "            elif c == ']':\n",
    "                curr = stack.pop()\n",
    "                stack[-1].add(curr)\n",
    "                i += 1 \n",
    "            elif c == ',':\n",
    "                i += 1\n",
    "        return stack[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "\n",
    "        if s[0] != '[':\n",
    "            return NestedInteger(int(s))\n",
    "\n",
    "        nestedList = NestedInteger()\n",
    "        i, stack = 1, []\n",
    "        for j in range(1, len(s)-1):\n",
    "            if s[j] == ',' and not stack:\n",
    "                cur = self.deserialize(s[i:j])\n",
    "                nestedList.add(cur)\n",
    "                i = j + 1\n",
    "            elif s[j] == '[':\n",
    "                stack.append('[')\n",
    "            elif s[j] == ']':\n",
    "                stack.pop()\n",
    "        if i < len(s) - 1:\n",
    "            last = self.deserialize(s[i: len(s)-1])\n",
    "            nestedList.add(last)\n",
    "        return nestedList\n",
    "                        \n",
    "                \n",
    "\n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "    \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        index =0 \n",
    "        def dfs()->NestedInteger:\n",
    "            nonlocal index\n",
    "            if s[index] == '[':\n",
    "                index+=1\n",
    "                ni = NestedInteger()\n",
    "                while s[index] != ']':\n",
    "                    ni.add(dfs())\n",
    "                    if s[index] == ',':\n",
    "                        index+=1\n",
    "                index+=1\n",
    "                return ni\n",
    "            else:\n",
    "                negative = False\n",
    "                if s[index] == '-':\n",
    "                    negative = True\n",
    "                    index += 1\n",
    "                num = 0\n",
    "                while index<len(s) and s[index].isdigit():\n",
    "                    num *= 10\n",
    "                    num += int(s[index])\n",
    "                    index += 1\n",
    "                if negative:\n",
    "                    num = -num\n",
    "                return NestedInteger(num)\n",
    "        return dfs()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        if s[0] != \"[\":\n",
    "            return NestedInteger(int(s))\n",
    "        stack = []\n",
    "        num = 0\n",
    "        negative = False\n",
    "        for i, c in enumerate(s):\n",
    "            if c == \"[\":\n",
    "                stack.append(NestedInteger())\n",
    "            elif c.isdigit():\n",
    "                num = num * 10 + int(c)\n",
    "            elif c == \"-\":\n",
    "                negative = True\n",
    "            elif c in \",]\":\n",
    "                if s[i - 1].isdigit():\n",
    "                    if negative:\n",
    "                        num = -num\n",
    "                    stack[-1].add(NestedInteger(num))\n",
    "                negative = False\n",
    "                num = 0\n",
    "                if c == \"]\" and len(stack) > 1:\n",
    "                    stack[-2].add(stack.pop())\n",
    "        return stack[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\r\n",
    "# This is the interface that allows for creating nested lists.\r\n",
    "# You should not implement it, or speculate about its implementation\r\n",
    "# \"\"\"\r\n",
    "#class NestedInteger:\r\n",
    "#    def __init__(self, value=None):\r\n",
    "#        \"\"\"\r\n",
    "#        If value is not specified, initializes an empty list.\r\n",
    "#        Otherwise initializes a single integer equal to value.\r\n",
    "#        \"\"\"\r\n",
    "#\r\n",
    "#    def isInteger(self):\r\n",
    "#        \"\"\"\r\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\r\n",
    "#        :rtype bool\r\n",
    "#        \"\"\"\r\n",
    "#\r\n",
    "#    def add(self, elem):\r\n",
    "#        \"\"\"\r\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\r\n",
    "#        :rtype void\r\n",
    "#        \"\"\"\r\n",
    "#\r\n",
    "#    def setInteger(self, value):\r\n",
    "#        \"\"\"\r\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\r\n",
    "#        :rtype void\r\n",
    "#        \"\"\"\r\n",
    "#\r\n",
    "#    def getInteger(self):\r\n",
    "#        \"\"\"\r\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\r\n",
    "#        Return None if this NestedInteger holds a nested list\r\n",
    "#        :rtype int\r\n",
    "#        \"\"\"\r\n",
    "#\r\n",
    "#    def getList(self):\r\n",
    "#        \"\"\"\r\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\r\n",
    "#        Return None if this NestedInteger holds a single integer\r\n",
    "#        :rtype List[NestedInteger]\r\n",
    "#        \"\"\"\r\n",
    "class Solution:\r\n",
    "    def deserialize(self, s: str) -> NestedInteger:\r\n",
    "        n = len(s)\r\n",
    "        i = 0\r\n",
    "        stack = []\r\n",
    "        while i < n:\r\n",
    "            if s[i] == '[':\r\n",
    "                stack.append(NestedInteger())\r\n",
    "                i += 1\r\n",
    "            elif s[i] == ']':\r\n",
    "                ni = stack.pop()\r\n",
    "                if stack:\r\n",
    "                    stack[-1].add(ni)\r\n",
    "                else:\r\n",
    "                    return ni\r\n",
    "                i += 1\r\n",
    "            elif s[i] == ',':\r\n",
    "                i += 1\r\n",
    "            else:\r\n",
    "                j = i\r\n",
    "                while j < n and s[j] not in ',[]':\r\n",
    "                    j += 1\r\n",
    "                ni = NestedInteger(int(s[i:j]))\r\n",
    "                if stack:\r\n",
    "                    stack[-1].add(ni)\r\n",
    "                else:\r\n",
    "                    return ni\r\n",
    "                i = j\r\n",
    "        return None\r\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        index = 0\n",
    "\n",
    "        def dfs() -> NestedInteger:\n",
    "            nonlocal index\n",
    "            if s[index] == '[':\n",
    "                index += 1\n",
    "                ni = NestedInteger()\n",
    "                while s[index] != ']':\n",
    "                    ni.add(dfs())\n",
    "                    if s[index] == ',':\n",
    "                        index += 1\n",
    "                index += 1\n",
    "                return ni\n",
    "            else:\n",
    "                tempString = ''\n",
    "                while index < len(s) and (s[index].isdigit() or s[index] == \"-\"):\n",
    "                    tempString = tempString +s[index]\n",
    "                    index += 1\n",
    "                num = int(tempString)\n",
    "                return NestedInteger(num)\n",
    "\n",
    "        return dfs()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "# class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        if s[0] != '[':\n",
    "            return  NestedInteger(int(s))\n",
    "        stack, num, negative = [], 0, False\n",
    "        for i, c in enumerate(s):\n",
    "            if c == '-':\n",
    "                negative = True\n",
    "            elif c.isdigit():\n",
    "                num = num * 10 + int(c)\n",
    "            elif c == '[':\n",
    "                # 左括号则新建NestedInteger\n",
    "                stack.append(NestedInteger())\n",
    "            elif c in ',]':\n",
    "                # 逗号则代表一个数字的结束，需要把它加在栈顶的NestedInteger上 which is还没初始值\n",
    "                # ] 代表一个NestedInteger的结束，要把这个NestedInteger（在栈顶）加在它下面的NestedInteger里\n",
    "                if s[i-1].isdigit():\n",
    "                    if negative:\n",
    "                        num *= -1 \n",
    "                    stack[-1].add(NestedInteger(num))\n",
    "                    num, negative = 0, False\n",
    "                if c == ']' and len(stack) > 1:\n",
    "                    stack[-2].add(stack.pop())\n",
    "        return stack.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        # 纯数字\n",
    "        if s[0] != '[':\n",
    "            return NestedInteger(int(s))\n",
    "        stack, curVal, sign = [], 0, False\n",
    "        for i, c in enumerate(s):\n",
    "            match c:\n",
    "                case '[':\n",
    "                    # 递归嵌套\n",
    "                    stack.append(NestedInteger())\n",
    "                case '-':\n",
    "                    # 数字符号\n",
    "                    sign = True\n",
    "                case ',':\n",
    "                    # 只有上一个字符是数字才加入了新的数字，否则可能是 \"],\"\n",
    "                    if s[i - 1].isdigit():\n",
    "                        stack[-1].add(NestedInteger(-curVal if sign else curVal))\n",
    "                    curVal, sign = 0, False\n",
    "                case ']':\n",
    "                    # 只有上一个字符是数字才加入了新的数字，否则可能是 \"[]\"\n",
    "                    if s[i - 1].isdigit():\n",
    "                        stack[-1].add(NestedInteger(-curVal if sign else curVal))\n",
    "                    # 弹出栈，并将当前的对象加入嵌套的列表中\n",
    "                    if len(stack) > 1:\n",
    "                        cur = stack.pop()\n",
    "                        stack[-1].add(cur)\n",
    "                    curVal, sign = 0, False\n",
    "                case _:\n",
    "                    # 数字计算\n",
    "                    curVal = curVal * 10 + int(c)\n",
    "        return stack.pop()\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        \n",
    "        idx = 0\n",
    "        \n",
    "        def dfs():\n",
    "            nonlocal idx\n",
    "            if s[idx] == \"[\":\n",
    "                ni = NestedInteger()\n",
    "                idx += 1\n",
    "                while s[idx] != \"]\":\n",
    "                    ni.add(dfs())\n",
    "                    if s[idx] == \",\":\n",
    "                        idx += 1\n",
    "                idx += 1\n",
    "                return ni\n",
    "            else:\n",
    "                neg = False\n",
    "                if s[idx] == \"-\":\n",
    "                    neg = True\n",
    "                    idx += 1\n",
    "                num = 0\n",
    "                while idx < len(s) and s[idx].isdigit():\n",
    "                    num = num * 10 + int(s[idx])\n",
    "                    idx += 1\n",
    "                if neg:\n",
    "                    num = -num\n",
    "                return NestedInteger(num)\n",
    "        \n",
    "        return dfs()\n",
    "                \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:        \n",
    "        lst = []\n",
    "        ans = 0\n",
    "        flag = False\n",
    "        neg_int = 1\n",
    "        for p in s:\n",
    "            if p == '[' or p == ']' or p == ',':\n",
    "                if flag:\n",
    "                    ans *= neg_int\n",
    "                    lst.append(ans)\n",
    "                    ans = 0\n",
    "                    neg_int = 1\n",
    "                    flag = False\n",
    "                if p != ',':\n",
    "                    lst.append(p)\n",
    "            elif p == '-':\n",
    "                neg_int = -1\n",
    "            else:\n",
    "                ans = ans * 10 + int(p)\n",
    "                flag = True\n",
    "        if flag:\n",
    "            ans *= neg_int\n",
    "            lst.append(ans)\n",
    "\n",
    "        stack = deque()\n",
    "        while lst:\n",
    "            op = lst.pop()\n",
    "            if op == ']':\n",
    "                stack.append(']')\n",
    "            elif op == '[':\n",
    "                nested = NestedInteger()\n",
    "                while stack:\n",
    "                    obj = stack.pop()\n",
    "                    if obj == ']':\n",
    "                        stack.append(nested)\n",
    "                        break\n",
    "                    nested.add(obj)\n",
    "            else:\n",
    "                stack.append(NestedInteger(op))\n",
    "        return stack.pop()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        i=0\n",
    "        ret=NestedInteger()\n",
    "        handled=[]\n",
    "        while i<len(s):\n",
    "            if s[i] in ('[',']',','):\n",
    "                handled.append(s[i])\n",
    "                i+=1\n",
    "            else:\n",
    "                left=i\n",
    "                while i<len(s) and s[i] not in ('[',']',','):\n",
    "                    i+=1\n",
    "                handled.append(int(s[left:i]))\n",
    "        depth=0\n",
    "        depth_temp=[]\n",
    "        for item in handled:\n",
    "            if item=='[':\n",
    "                depth+=1\n",
    "                depth_temp.append(NestedInteger())\n",
    "            elif item==']':\n",
    "                depth-=1\n",
    "                temp=depth_temp.pop()\n",
    "                if len(depth_temp)!=0:\n",
    "                    depth_temp[-1].add(temp)\n",
    "                else:\n",
    "                    ret=temp\n",
    "\n",
    "            elif item==',':\n",
    "                pass\n",
    "            else:\n",
    "                if depth==0:\n",
    "                    ret.setInteger(item)\n",
    "                else:\n",
    "                    temp=NestedInteger()\n",
    "                    temp.setInteger(item)\n",
    "                    if len(depth_temp)!=0:\n",
    "                        depth_temp[-1].add(temp)\n",
    "                    else:\n",
    "                        ret.add(temp)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "\n",
    "        if s[0] != '[':\n",
    "            return NestedInteger(int(s))\n",
    "\n",
    "        nestedList = NestedInteger()\n",
    "        i, stack = 1, []\n",
    "        for j in range(1, len(s)-1):\n",
    "            if s[j] == ',' and not stack:\n",
    "                cur = self.deserialize(s[i:j])\n",
    "                nestedList.add(cur)\n",
    "                i = j + 1\n",
    "            elif s[j] == '[':\n",
    "                stack.append('[')\n",
    "            elif s[j] == ']':\n",
    "                stack.pop()\n",
    "        if i < len(s) - 1:\n",
    "            nestedList.add(self.deserialize(s[i: len(s)-1]))\n",
    "        return nestedList\n",
    "                        \n",
    "                \n",
    "\n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "    \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\r\n",
    "# This is the interface that allows for creating nested lists.\r\n",
    "# You should not implement it, or speculate about its implementation\r\n",
    "# \"\"\"\r\n",
    "#class NestedInteger:\r\n",
    "#    def __init__(self, value=None):\r\n",
    "#        \"\"\"\r\n",
    "#        If value is not specified, initializes an empty list.\r\n",
    "#        Otherwise initializes a single integer equal to value.\r\n",
    "#        \"\"\"\r\n",
    "#\r\n",
    "#    def isInteger(self):\r\n",
    "#        \"\"\"\r\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\r\n",
    "#        :rtype bool\r\n",
    "#        \"\"\"\r\n",
    "#\r\n",
    "#    def add(self, elem):\r\n",
    "#        \"\"\"\r\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\r\n",
    "#        :rtype void\r\n",
    "#        \"\"\"\r\n",
    "#\r\n",
    "#    def setInteger(self, value):\r\n",
    "#        \"\"\"\r\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\r\n",
    "#        :rtype void\r\n",
    "#        \"\"\"\r\n",
    "#\r\n",
    "#    def getInteger(self):\r\n",
    "#        \"\"\"\r\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\r\n",
    "#        Return None if this NestedInteger holds a nested list\r\n",
    "#        :rtype int\r\n",
    "#        \"\"\"\r\n",
    "#\r\n",
    "#    def getList(self):\r\n",
    "#        \"\"\"\r\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\r\n",
    "#        Return None if this NestedInteger holds a single integer\r\n",
    "#        :rtype List[NestedInteger]\r\n",
    "#        \"\"\"\r\n",
    "class Solution:\r\n",
    "    def deserialize(self, s: str) -> NestedInteger:\r\n",
    "        # 纯数字\r\n",
    "        if s[0] != '[':\r\n",
    "            return NestedInteger(int(s))\r\n",
    "        \r\n",
    "        stack,curVal,sign = [], 0, False\r\n",
    "        for i, c in enumerate(s):\r\n",
    "            match c:\r\n",
    "                case '[':\r\n",
    "                    # 嵌套递归\r\n",
    "                    stack.append(NestedInteger())\r\n",
    "                case '-':\r\n",
    "                    # 符号\r\n",
    "                    sign = True\r\n",
    "                case ',':\r\n",
    "                    #上一个是数字则表示加入了新的数字,另一种情况是\"],\"\r\n",
    "                    if s[i-1].isdigit():\r\n",
    "                        stack[-1].add(NestedInteger(-curVal if sign else curVal))\r\n",
    "                    curVal,sign = 0, False\r\n",
    "                case ']':\r\n",
    "                    # 只有上一个字符是数字才加入了新的数字，否则可能是 \"[]\"\r\n",
    "                    if s[i-1].isdigit():\r\n",
    "                        stack[-1].add(NestedInteger(-curVal if sign else curVal))\r\n",
    "                    # 出栈，并将当前的对象加入嵌套的列表中\r\n",
    "                    if len(stack) > 1:\r\n",
    "                        cur = stack.pop()\r\n",
    "                        stack[-1].add(cur)\r\n",
    "                    curVal,sign = 0,False\r\n",
    "                # case _相当于else\r\n",
    "                case _:\r\n",
    "                    curVal = curVal * 10 + int(c)\r\n",
    "        return stack.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        if s[0].isdigit():\n",
    "            return NestedInteger(int(s))\n",
    "        if s[0] == \"-\":\n",
    "            return NestedInteger(-int(s[1:]))\n",
    "        stk = []\n",
    "        i = 0\n",
    "        n = len(s)\n",
    "        num = 0\n",
    "        flag = False\n",
    "        while i < n:\n",
    "            o = s[i]\n",
    "            if o == \"-\":\n",
    "                i += 1\n",
    "                flag = True\n",
    "            if o == \"[\":\n",
    "                stk.append(NestedInteger())\n",
    "                i += 1\n",
    "            if o.isdigit():\n",
    "                num = 0\n",
    "                while i < n and s[i].isdigit():\n",
    "                    num = num * 10 + int(s[i])\n",
    "                    i += 1\n",
    "                if flag:\n",
    "                    num = -num\n",
    "            if o == \",\":\n",
    "                if s[i - 1].isdigit():\n",
    "                    stk[-1].add(NestedInteger(num))\n",
    "                flag = False\n",
    "                i += 1\n",
    "            if o == \"]\":\n",
    "                if s[i - 1].isdigit():\n",
    "                    stk[-1].add(NestedInteger(num))\n",
    "                flag = False\n",
    "\n",
    "                if len(stk) > 1:\n",
    "                    l = stk.pop()\n",
    "                    stk[-1].add(l)\n",
    "                flag = False\n",
    "                i += 1\n",
    "\n",
    "        return stk[-1]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "\n",
    "        if s[0] != '[':\n",
    "            return NestedInteger(int(s))\n",
    "\n",
    "        nestedList = NestedInteger()\n",
    "        i, stack = 1, []\n",
    "        for j in range(1, len(s)-1):\n",
    "            if s[j] == ',' and not stack:\n",
    "                cur = self.deserialize(s[i:j])\n",
    "                nestedList.add(cur)\n",
    "                i = j + 1\n",
    "            elif s[j] == '[':\n",
    "                stack.append('[')\n",
    "            elif s[j] == ']':\n",
    "                stack.pop()\n",
    "        if i < len(s) - 1:\n",
    "            nestedList.add(self.deserialize(s[i: len(s)-1]))\n",
    "        return nestedList\n",
    "                        \n",
    "                \n",
    "\n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "    \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        stack = []\n",
    "        sign = 1\n",
    "        lastnum = None\n",
    "        for i, ch in enumerate(s):\n",
    "            if ch.isdigit():\n",
    "                if not lastnum: lastnum = 0\n",
    "                lastnum = lastnum * 10 + int(ch)\n",
    "            elif ch == \"-\":\n",
    "                sign = -1\n",
    "            elif ch == \",\":\n",
    "                if i > 0 and s[i - 1] == \"]\":\n",
    "                    continue\n",
    "                stack[-1].add(NestedInteger(sign * lastnum))\n",
    "                sign = 1\n",
    "                lastnum = None\n",
    "            elif ch == \"[\":\n",
    "                stack.append(NestedInteger())\n",
    "                sign = 1\n",
    "                lastnum = None\n",
    "            else:\n",
    "                if lastnum is not None: \n",
    "                    stack[-1].add(NestedInteger(sign * lastnum))\n",
    "                    sign = 1\n",
    "                    lastnum = None\n",
    "                lastitem = stack.pop()\n",
    "                if stack: \n",
    "                    stack[-1].add(lastitem)\n",
    "                else:\n",
    "                    stack.append(lastitem)\n",
    "        if lastnum: stack.append(NestedInteger(sign * lastnum))\n",
    "        return stack[0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transfer(self,stk):\n",
    "        res = NestedInteger()\n",
    "        for ele in stk:\n",
    "            if type(ele) is not list:\n",
    "                res.add(NestedInteger(ele))\n",
    "            else:\n",
    "                res.add(self.transfer(ele))\n",
    "        return res\n",
    "            \n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        stk  =[]\n",
    "        num =0 \n",
    "        pos = 1\n",
    "        \n",
    "        \n",
    "        for i in range(len(s)):\n",
    "            if s[i]==\"[\":\n",
    "                stk.append(\"[\")\n",
    "            elif s[i] ==\",\":\n",
    "                if s[i-1].isdigit():\n",
    "                    stk.append(num*pos)\n",
    "                    num = 0\n",
    "                    pos = 1\n",
    "                else:\n",
    "                    pass\n",
    "            elif s[i]==\"-\":\n",
    "                pos = -1\n",
    "            elif s[i] == \"]\":\n",
    "                if s[i-1].isdigit():\n",
    "                    stk.append(num*pos)\n",
    "                    num = 0\n",
    "                    pos = 1\n",
    "                tmp_res = []\n",
    "                while stk:\n",
    "                    ele= stk.pop()\n",
    "                    if ele ==\"[\":\n",
    "                        break\n",
    "                    else:\n",
    "                        tmp_res.insert(0,ele)\n",
    "                stk.append(tmp_res)\n",
    "            else:\n",
    "                num = num*10+int(s[i])\n",
    "        #print(stk,num)\n",
    "        if stk:\n",
    "            res = self.transfer(stk[0])\n",
    "        else:\n",
    "            num = num*pos\n",
    "            res = NestedInteger()\n",
    "            res.setInteger(num)\n",
    "             # s是纯数字\n",
    "        return res\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        index = 0\n",
    "\n",
    "        def dfs() -> NestedInteger:\n",
    "            nonlocal index\n",
    "            if s[index] == '[':\n",
    "                index += 1\n",
    "                ni = NestedInteger()\n",
    "                while s[index] != ']':\n",
    "                    ni.add(dfs())\n",
    "                    if s[index] == ',':\n",
    "                        index += 1\n",
    "                index += 1\n",
    "                return ni\n",
    "            else:\n",
    "                negative = False\n",
    "                if s[index] == '-':\n",
    "                    negative = True\n",
    "                    index += 1\n",
    "                num = 0\n",
    "                while index < len(s) and s[index].isdigit():\n",
    "                    num *= 10\n",
    "                    num += int(s[index])\n",
    "                    index += 1\n",
    "                if negative:\n",
    "                    num = -num\n",
    "                return NestedInteger(num)\n",
    "\n",
    "        return dfs()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        nest = NestedInteger()\n",
    "\n",
    "        def stringToInt(s):\n",
    "            val = 0\n",
    "            neg_flag = False\n",
    "            if s[0] == '-':\n",
    "                neg_flag = True\n",
    "                s = s[1:]\n",
    "            for ch in s:\n",
    "                asi_val = ord(ch)\n",
    "                if asi_val >=ord('0') and asi_val <= ord('9'):\n",
    "                    val = val * 10 + asi_val - ord('0')\n",
    "                else:\n",
    "                    break\n",
    "            if neg_flag == True:\n",
    "                val = 0 - val\n",
    "            return val\n",
    "\n",
    "        if s[0] != '[':\n",
    "            return NestedInteger(stringToInt(s))\n",
    "            \n",
    "        ind = 1\n",
    "        while ind < len(s):\n",
    "            if s[ind] == '[':\n",
    "                #tmp_stack = self.deserialize(s[ind:]).getList()\n",
    "                #nest.add(tmp_stack)\n",
    "                tmp_stack = self.deserialize(s[ind:])\n",
    "                nest.add(tmp_stack)\n",
    "                diff = 1\n",
    "                ind += 1\n",
    "                while diff != 0:\n",
    "                    if s[ind] == '[':\n",
    "                        diff += 1\n",
    "                    elif s[ind] == ']':\n",
    "                        diff -= 1\n",
    "                    ind += 1\n",
    "            elif s[ind] == ',':\n",
    "                ind += 1\n",
    "            elif s[ind] == ']':\n",
    "                return nest\n",
    "            else:\n",
    "                val = stringToInt(s[ind:])\n",
    "                nest.add(NestedInteger(val))\n",
    "                while ind < len(s) and s[ind] not in '[,]':\n",
    "                    ind += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        if s[0] != '[':\n",
    "            return NestedInteger(int(s))\n",
    "        stack, num, negative = [], 0, False\n",
    "        for i, c in enumerate(s):\n",
    "            if c == '-':\n",
    "                negative = True\n",
    "            elif c.isdigit():\n",
    "                num = num * 10 + int(c)\n",
    "            elif c == '[':\n",
    "                stack.append(NestedInteger())\n",
    "            elif c in ',]':\n",
    "                if s[i-1].isdigit():\n",
    "                    if negative:\n",
    "                        num = -num\n",
    "                    stack[-1].add(NestedInteger(num))\n",
    "                num, negative = 0, False\n",
    "                if c == ']' and len(stack) > 1:\n",
    "                    stack[-2].add(stack.pop())\n",
    "        return stack.pop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        stack = []\n",
    "        sign = 1\n",
    "        lastnum = -1\n",
    "        for i, ch in enumerate(s):\n",
    "            if ch.isdigit():\n",
    "                if lastnum == -1: \n",
    "                    lastnum = 0\n",
    "                lastnum = lastnum * 10 + int(ch)\n",
    "            elif ch == \"-\":\n",
    "                sign = -1\n",
    "            elif ch == \",\":\n",
    "                if i > 0 and s[i - 1] == \"]\":\n",
    "                    continue\n",
    "                stack[-1].add(NestedInteger(sign * lastnum))\n",
    "                sign = 1\n",
    "                lastnum = -1\n",
    "            elif ch == \"[\":\n",
    "                stack.append(NestedInteger())\n",
    "                sign = 1\n",
    "                lastnum = -1\n",
    "            else:\n",
    "                if lastnum != -1: \n",
    "                    stack[-1].add(NestedInteger(sign * lastnum))\n",
    "                    sign = 1\n",
    "                    lastnum = -1\n",
    "                lastitem = stack.pop()\n",
    "                if stack: \n",
    "                    stack[-1].add(lastitem)\n",
    "                else:\n",
    "                    stack.append(lastitem)\n",
    "        if lastnum: stack.append(NestedInteger(sign * lastnum))\n",
    "        return stack[0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        if s[0] != '[':\n",
    "            return NestedInteger(int(s))\n",
    "        stack, num, negative = [], 0, False\n",
    "        for i, c in enumerate(s):\n",
    "            if c == '-':\n",
    "                negative = True\n",
    "            elif c.isdigit():\n",
    "                num = num * 10 + int(c)\n",
    "            elif c == '[':\n",
    "                stack.append(NestedInteger())\n",
    "            elif c in ',]':\n",
    "                if s[i-1].isdigit():\n",
    "                    if negative:\n",
    "                        num = -num\n",
    "                    stack[-1].add(NestedInteger(num))\n",
    "                num, negative = 0, False\n",
    "                if c == ']' and len(stack) > 1:\n",
    "                    stack[-2].add(stack.pop())\n",
    "        return stack.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        if s[0]!='[':\n",
    "            return NestedInteger(int(s))\n",
    "        stk, curVal, sign=[], 0, False\n",
    "        for i, c in enumerate(s):\n",
    "            match c:\n",
    "                case \"[\":\n",
    "                    stk.append(NestedInteger())\n",
    "                case \"-\":\n",
    "                    sign=True\n",
    "                case \",\":\n",
    "                    if s[i-1].isdigit():\n",
    "                        stk[-1].add(NestedInteger(-curVal if sign else curVal))\n",
    "                    curVal, sign=0, False\n",
    "                case \"]\":\n",
    "                    if s[i-1].isdigit():\n",
    "                        stk[-1].add(NestedInteger(-curVal if sign else curVal))\n",
    "                    if len(stk)>1:\n",
    "                        cur=stk.pop()\n",
    "                        stk[-1].add(cur)\n",
    "                    curVal, sign=0, False\n",
    "                case _:\n",
    "                    curVal = curVal*10+int(c)\n",
    "        return stk.pop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        l = re.split('(\\[|\\]|,)',s)\n",
    "        l = list(filter(lambda x:len(x),l))\n",
    "        stack = []\n",
    "        for i in l:\n",
    "            if i == ',':\n",
    "                continue\n",
    "            elif i == ']':\n",
    "                n = NestedInteger()\n",
    "                tmp = []\n",
    "                while(stack[-1]!='['):\n",
    "                    tmp.append(stack[-1])\n",
    "                    stack = stack[:-1]\n",
    "                while(len(tmp)):\n",
    "                    n.add(tmp[-1])\n",
    "                    tmp = tmp[:-1]\n",
    "                stack[-1] = n\n",
    "                # pop \n",
    "            elif i == '[':\n",
    "                stack.append(i)\n",
    "            else:\n",
    "                stack.append(NestedInteger(int(i)))\n",
    "        print(len(stack))\n",
    "        return stack[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        return NestedInteger(eval(s))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        return NestedInteger(eval(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        # print(eval(s))\n",
    "        a = eval(s)\n",
    "        n = NestedInteger(a)\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        # 栈\n",
    "        # if s[0] != \"[\":\n",
    "        #     return NestedInteger(int(s))\n",
    "        # stack = []\n",
    "        # num = 0\n",
    "        # negative = False\n",
    "        # for i, c in enumerate(s):\n",
    "        #     if c == \"-\":\n",
    "        #         negative = True\n",
    "        #     elif c.isdigit():\n",
    "        #         num = num*10 + int(c)\n",
    "        #     elif c == \"[\":\n",
    "        #         stack.append(NestedInteger())\n",
    "        #     elif c in \",]\":\n",
    "        #         if s[i-1].isdigit():\n",
    "        #             if negative:\n",
    "        #                 num = -num\n",
    "        #             stack[-1].add(NestedInteger(num))\n",
    "        #         num, negative = 0, False\n",
    "        #         if c == \"]\" and len(stack)>1:\n",
    "        #             stack[-2].add(stack.pop())\n",
    "        # return stack.pop()\n",
    "\n",
    "\n",
    "        # python 流\n",
    "        return NestedInteger(eval(s))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        return NestedInteger(eval(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "def getn(l):\n",
    "    ret = NestedInteger()\n",
    "    for i in l:\n",
    "        if isinstance(i,int):\n",
    "            ret.add(NestedInteger(i))\n",
    "        else:\n",
    "            ret.add(getn(i))\n",
    "    return ret\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        l = eval(s)\n",
    "        if isinstance(l,int):\n",
    "            return NestedInteger(l)\n",
    "        return getn(l)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# \"\"\"\n",
    "# This is the interface that allows for creating nested lists.\n",
    "# You should not implement it, or speculate about its implementation\n",
    "# \"\"\"\n",
    "#class NestedInteger:\n",
    "#    def __init__(self, value=None):\n",
    "#        \"\"\"\n",
    "#        If value is not specified, initializes an empty list.\n",
    "#        Otherwise initializes a single integer equal to value.\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def isInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return True if this NestedInteger holds a single integer, rather than a nested list.\n",
    "#        :rtype bool\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def add(self, elem):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def setInteger(self, value):\n",
    "#        \"\"\"\n",
    "#        Set this NestedInteger to hold a single integer equal to value.\n",
    "#        :rtype void\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getInteger(self):\n",
    "#        \"\"\"\n",
    "#        @return the single integer that this NestedInteger holds, if it holds a single integer\n",
    "#        Return None if this NestedInteger holds a nested list\n",
    "#        :rtype int\n",
    "#        \"\"\"\n",
    "#\n",
    "#    def getList(self):\n",
    "#        \"\"\"\n",
    "#        @return the nested list that this NestedInteger holds, if it holds a nested list\n",
    "#        Return None if this NestedInteger holds a single integer\n",
    "#        :rtype List[NestedInteger]\n",
    "#        \"\"\"\n",
    "class Solution:\n",
    "    def deserialize(self, s: str) -> NestedInteger:\n",
    "        def dfs(s):\n",
    "            if isinstance(s,list):\n",
    "                ans = NestedInteger()\n",
    "                for i in s:\n",
    "                    ans.add(dfs(i))\n",
    "                return ans\n",
    "            elif isinstance(s,int):\n",
    "                return NestedInteger(s)\n",
    "            raise ValueError()\n",
    "        return dfs(eval(s))"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
